package com.example.hanfu_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hanfu_backend.entity.Customer;
import com.example.hanfu_backend.entity.HanfuShop;
import com.example.hanfu_backend.mapper.CustomerMapper;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerMapper customerMapper;

    @GetMapping("/getCustomers")
    @Operation(summary = "分页查询所有用户")
    public Map<String,Object> getCustomers(@RequestParam("pageNum")Integer pageNum, @RequestParam("pageSize")Integer pageSize) {
        Page<Customer> page = new Page<>(pageNum,pageSize); // 创建分页对象
        // 设置分页查询条件
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("customer_id");
        IPage<Customer> iPage = customerMapper.selectPage(page,queryWrapper); // 执行分页查询操作
        // 封装分页查询结果
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getCustomerById")
    @Operation(summary = "通过用户Id分页模糊查询用户")
    public Map<String,Object> queryCustomerById(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("customerId")Integer customerId) {
        Page<Customer> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("customer_id",customerId);
        IPage<Customer> iPage = customerMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getCustomerByUsername")
    @Operation(summary = "通过用户名分页模糊查询用户")
    public Map<String,Object> queryCustomerByUsername(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("customerUsername")String customerUsername) {
        Page<Customer> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("customer_username",customerUsername);
        IPage<Customer> iPage = customerMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getCustomerByPhone")
    @Operation(summary = "通过手机号分页模糊查询用户")
    public Map<String,Object> queryCustomerByPhone(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("customerPhone")Integer customerPhone) {
        Page<Customer> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("customer_phone",customerPhone);
        IPage<Customer> iPage = customerMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getCustomerByPhoneNoPage")
    @Operation(summary = "通过手机号查询用户")
    public List<Customer> queryCustomerByPhoneNoPage(@RequestParam("customerPhone") String customerPhone) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_phone",customerPhone);
        return customerMapper.selectList(queryWrapper);
    }

    @GetMapping("/getCustomerByIdNoPage")
    @Operation(summary = "通过Id查询用户")
    public List<Customer> queryCustomerByIdNoPage(@RequestParam("customerId") Integer customerId) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id",customerId);
        return customerMapper.selectList(queryWrapper);
    }

    @GetMapping("/toLogin")
    @Operation(summary = "登录")
    public List<Customer> toLogin(@RequestParam("customerPhone")String customerPhone,@RequestParam("customerPassword")String customerPassword) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_phone",customerPhone).eq("customer_password",customerPassword);
        return customerMapper.selectList(queryWrapper);
    }

    @DeleteMapping("/deleteCustomer")
    @Operation(summary = "根据ID删除某一用户")
    public int deleteCustomer(@RequestParam("customerId")Integer customerId) {
        return customerMapper.deleteById(customerId);
    }

    @PostMapping("/insertCustomer")
    @Operation(summary = "添加用户")
    public int insertCustomer(@RequestBody Customer customer) {
        Random random = new Random();
        String number = String.valueOf(random.nextInt(900000) + 100000);
        while(number.charAt(0) == '0' && queryCustomerByIdNoPage(Integer.parseInt(number)) != null) {
            number = String.valueOf(random.nextInt(900000) + 100000);
        }
        customer.setCustomerId(Integer.parseInt(number));
        // 该手机号已注册
        if(queryCustomerByPhoneNoPage(customer.getCustomerPhone()).size() > 0) return 0;
        customer.setCustomerBirthday(LocalDate.parse("2003-08-29"));
        customer.setCustomerGender('女');
        customer.setCustomerHeadImage("202405152326373ecbf69d027b4bd2a3c312bcec2c7c6e.jpg");
        customer.setCustomerUsername("游客");
        return customerMapper.insert(customer);
    }

    @PutMapping("/updateCustomer")
    @Operation(summary = "根据Id修改某一用户")
    public int updateCustomer(@RequestBody Customer customer) {
        return customerMapper.updateById(customer);
    }
}
